package com.tebiecloud.mpopen.server.controller;

import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import com.tebiecloud.mpopen.client.model.mpopen.ApiQueryAuth;
import com.tebiecloud.mpopen.client.model.mpopen.AuthorizerInfo;
import com.tebiecloud.mpopen.client.model.mpopen.PreAuthCode;
import com.tebiecloud.mpopen.server.service.MpopenApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 第三方平台接口
 * @Author: tebie
 * @Date: 2019-09-18 16:13
 */
@Slf4j
@RestController
@RequestMapping(value = "/mpopenapi")
public class MpopenApiController {

    @Autowired
    private MpopenApiService mpopenApiService;

    /**
     * 获取预授权码pre_auth_code
     * @return
     */
    @PostMapping("infoPreAuthCode")
    public ResultBody<PreAuthCode> getPreAuthCode() {
        PreAuthCode preAuthCode = mpopenApiService.getPreAuthCode();
        return ResultBodyUtils.ok(preAuthCode);
    }

    /**
     * 使用授权码换取公众号的接口调用凭据和授权信息
     * @param authCode
     * @return
     */
    @PostMapping("infoApiQueryAuth")
    public ResultBody getApiQueryAuth(@RequestParam("authCode") String authCode) {
        ApiQueryAuth apiQueryAuth = mpopenApiService.getApiQueryAuth(authCode);
        return ResultBodyUtils.ok(apiQueryAuth);
    }

    /**
     * 获取授权方的公众号帐号基本信息
     * @param authorizerAppid
     * @return
     */
    @PostMapping("infoAuthorizerInfo")
    public ResultBody getAuthorizerInfo(@RequestParam("authorizerAppid") String authorizerAppid) {
        AuthorizerInfo authorizerInfo = mpopenApiService.getAuthorizerInfo(authorizerAppid);
        return ResultBodyUtils.ok(authorizerInfo);
    }

}
